﻿using System;
using System.IO;
using Newtonsoft.Json;

namespace Business.Entities
{
    public enum FontSizeEnum
    {
        Small = 0,
        Medium = 1,
        Large = 2
    }

    public class Config
    {
        [JsonIgnore]
        private const string ConfigFile = "config.json";
        public CookieCollection Cookies;
        
        public string Username { get; set; }
        /// <summary>
        /// True if stored account is a google apps account, and alternative URL is to be used in communication
        /// </summary>
        public bool IsGoogleAppsAccount { get; set; }
        /// <summary>
        /// Domain name for google apps account, e.g. "mysite.com"
        /// </summary>
        public string GoogleAppsDomain { get; set; }

        public string GoogleAppsDomainOrNull
        {
            get { return IsGoogleAppsAccount ? GoogleAppsDomain : null; }
        }

            public string UsernameText
        {
            get { return Username ?? "(No one)"; }

        }
        /// <summary>
        /// True when user has accepted license
        /// </summary>
        public bool IsLicenseAccepted { get; set; }
        
        private bool _outlookWinsSyncConflict = true;
        /// <summary>
        /// When changes are made both on google and on outlook mobile, 
        /// i have no way of knowing which is updated lates since outlook mobile
        /// dont timestamp changes. Result is that we have to make a choice
        /// based on this user setting
        /// </summary>
        public bool OutlookWinsSyncConflict
        {
            get { return _outlookWinsSyncConflict; }
            set { _outlookWinsSyncConflict = value; }
        }

        /// <summary>
        /// (Mobile) Max # task to display per page
        /// </summary>
        public int MaxTasksPerPage  { get; set; }
        /// <summary>
        /// (Mobile) true if completed tasks are to be hidden
        /// </summary>
        public bool HideCompletedTassk { get; set; }
        
        public bool RightHandedCheckBoxes { get; set; }
        public bool AutoSyncOnClose { get; set; }
        public bool AutoSyncOnStart { get; set; }
        public bool AutoSyncInBackground { get; set; }
        /// <summary>
        /// If AutoSyncInBackground, sync every n minutes
        /// </summary>
        public int AutoSyncIntervalMinutes { get; set; }
        public bool HideCompletedTasks { get; set; }

        /// <summary>
        /// Size of font. Default = Large
        /// </summary>
        public FontSizeEnum FontSize { get; set; }

        public int ImageSize { get; set; }

        [JsonIgnore]
        public bool IsNew;


        public Config()
        {
            AutoSyncOnClose = false;
            AutoSyncOnStart = true;
            MaxTasksPerPage = 30;
            FontSize = FontSizeEnum.Large;
        }

        public static Config Load()
        {
            string filename = Context.ConfigPath + "/" + ConfigFile;
            try {
                using (var stream = File.OpenRead(filename))
                {
                    var sr = new StreamReader(stream);
                    string text = sr.ReadToEnd();
                    Config cfg = Context.JsonApi.Deserialize<Config>(text);
                    sr.Close();
                    return cfg;
                }
            }
            catch (Exception)
            {
                var cfg =  new Config();
                cfg.IsNew = true;
                return cfg;
            }
        }

        public static void Wipe()
        {
            string filename = Context.ConfigPath + "/" + ConfigFile;
            if (File.Exists(filename)) File.Delete(filename);
        }

        public void Save()
        {
            using (var stream = File.Create(Context.ConfigPath + "/" + ConfigFile))
            {
                var sw = new StreamWriter(stream);
                var text = Context.JsonApi.Serialize(this);
                sw.Write(text);
                sw.Close();
            }
        }
    }
}
